home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / src / testcos.cc < prev    next >
C/C++ Source or Header  |  1989-08-18  |  3KB  |  102 lines

  1. /*
  2.  *    Test FFT routines; cosine transform
  3.  *
  4.  *    Copyright (C) 1988, 1989.
  5.  *
  6.  *    Dr. Thomas Keffer
  7.  *    Rogue Wave Associates
  8.  *    P.O. Box 85341
  9.  *    Seattle WA 98145-1341
  10.  *
  11.  *    Permission to use, copy, modify, and distribute this
  12.  *    software and its documentation for any purpose and
  13.  *    without fee is hereby granted, provided that the
  14.  *    above copyright notice appear in all copies and that
  15.  *    both that copyright notice and this permission notice
  16.  *    appear in supporting documentation.
  17.  *    
  18.  *    This software is provided "as is" without any
  19.  *    expressed or implied warranty.
  20.  *
  21.  *
  22.  *    @(#)testcos.cc    2.1    8/18/89
  23.  */
  24.  
  25.  
  26. #include "rw/DCosineFFT.h"
  27. #include "rw/Timer.h"
  28.  
  29. static void
  30. printVec(ostream& s, const DoubleVec& x);
  31.  
  32. main()
  33. {
  34.   // N must be even:
  35.   const unsigned N = 12;
  36.   cout <<"Testing DoubleCosineServer (Double Precision Cosine Server)\n\n";
  37.  
  38.   double twoN = 2*N;
  39.   DoubleCosineServer     server;
  40.  
  41.   // This will make half of a two period cosine wave;
  42.   DoubleVec a2cos(cos(DoubleVec(N+1, 0, 4.0*M_PI/twoN)));
  43.   DoubleVec a2sin(sin(DoubleVec(N-1, 4.0*M_PI/twoN, 4.0*M_PI/twoN)));
  44.   a2cos += 1.0;
  45.  
  46.   cout <<"**************************************\n";
  47.   cout <<"a2cos (one half of a two period cosine wave, plus 1.0):\n";
  48.   printVec(cout,a2cos);
  49.   cout <<"a2sin (one half of a two period sine wave):\n";
  50.   printVec(cout,a2sin);
  51.  
  52.   cout <<"**************************************\n";
  53.   cout <<"Checking cosine transform.\n";
  54.   cout <<"\nTransform of a2cos:\n";
  55.   DoubleVec a2cos_transform = server.cosine(a2cos)/twoN;
  56.   printVec(cout, a2cos_transform);
  57.  
  58.   cout <<"\nChecking Parseval's theorem for cosine transform.\n";
  59.   cout <<"Original variance: "<<variance(expandEven(a2cos))<<NL;
  60.  
  61.   // expand to full series:
  62.   double var = spectralVariance(expandEven(a2cos_transform));
  63.   cout <<"Spectral variance: "<<var<<NL;
  64.  
  65.   cout <<"\nChecking Nyquist for cosine transform.\n";
  66.   DoubleVec Nyquist(N+1,1.0);
  67.   Nyquist.slice(1,(N+1)/2,2) = -1.0;
  68.   cout <<"Original sequence:\n";
  69.   printVec(cout, Nyquist);
  70.   cout <<"Its transform:\n";
  71.   DoubleVec Nyquist_transform = server.cosine(Nyquist)/twoN;
  72.   printVec(cout, Nyquist_transform);
  73.   
  74.   cout <<"**************************************\n";
  75.   cout <<"Checking sine transform.\n";
  76.   cout <<"\nTransform of a2sin:\n";
  77.   DoubleVec a2sin_transform = server.sine(a2sin)/twoN;
  78.   printVec(cout, a2sin_transform);
  79.  
  80.   cout <<"\nChecking Parseval's theorem for sine transform.\n";
  81.   cout <<"Original variance: "<<variance(expandOdd(a2sin))<<NL;
  82.  
  83.   // expand to full series:
  84.   var = spectralVariance(expandOdd(a2sin_transform));
  85.   cout <<"Spectral variance: "<<var<<NL;
  86.   exit(0);
  87. }
  88.  
  89. // Use special functions to print vectors, to round the output
  90. // to a fixed point number, so that differences at the machine
  91. // precision won't be seen.
  92.  
  93. static void
  94. printVec(ostream& s, const DoubleVec& x)
  95. {
  96.   for(int i = 0; i<x.length(); i++){
  97.     if(!(i%5) && i) s<<"\n";
  98.     s << form("%8.5f",x(i)) << " ";
  99.   }
  100.   s<<"\n";
  101. }
  102.